//
//  SEGMenberPointsViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/5/28.
//

#import "SEGMenberPointsViewModel.h"

@implementation SEGMenberPointsViewModel
#pragma mark - business
-(void)loadDataintegral:(NSString*)userID completed:(void(^)(NSString*all,NSString*today,NSString*yesterday))completed{
    [self.managerIntegral requestIntegralStatisticsUserId:userID Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            SEGStatisticsModel* tmp = self.managerIntegral.seg_segstatisticsmodel;
            // 34839 【5.0】IOS 积分数据显示错误
            completed([tmp.all seperateNumberToComma],[tmp.today seperateNumberToComma],[tmp.yesterday seperateNumberToComma]);
            return;
        }
        completed(@"",@"",@"");
    }];
}

-(void)loadData_Integral:(void(^)(void))complted{
    [self.managerIntegral requestMyCardV2WithDict:nil Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if(isSuccessed){
            __block CGFloat cashTotal = 0.f;
            
            NSMutableArray<SEGBanalcesModel*>* arrM = [NSMutableArray<SEGBanalcesModel*> array];
            
            ///遍历卡券数据源
            [self.managerIntegral.seg_segbanalcesmodel enumerateObjectsUsingBlock:^(SEGBanalcesModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                //balancetype：1、2 优惠券
                if (self.banalceType == SEGBanalceType_Banalce_Cash ||self.banalceType == SEGBanalceType_Banalce_Time) {
                    if ([obj.balancetype integerValue] == SEGBanalceType_Banalce_Cash ||[obj.balancetype integerValue] == SEGBanalceType_Banalce_Time ) {
                        cashTotal += [obj.balance floatValue]/100.f;
                        [arrM addObject:obj];
                    }
                }else{
                    if ([obj.balancetype integerValue] == self.banalceType) {
                        cashTotal += [obj.balance floatValue]/100.f;
                        [arrM addObject:obj];
                    }
                }
            }];
            self.dataSource = arrM;
        }
        complted();
    }];
}

-(void)loadBalanceForHb:(void(^)(BOOL hasRecord,CGFloat cashTotal))completed{
    [self.managerIntegral requestMyBalanceForHbDict:nil Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        BOOL hasRecord = YES;
        __block CGFloat cashTotal = 0.F;
        if (isSuccessed) {
            if (self.managerIntegral.seg_segbalanceforhbmodel.count == 0) {
                hasRecord = NO;
            }else {
                [self.managerIntegral.seg_segbalanceforhbmodel enumerateObjectsUsingBlock:^(SEGBalanceForHbModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    cashTotal += obj.amount.floatValue;
                }];
            }
        }else {
            hasRecord = NO;
        }
        completed(hasRecord,cashTotal);
    }];
}

#pragma mark - getter
-(SEGIntegralManager *)managerIntegral{
    if (!_managerIntegral) {
        _managerIntegral = [[SEGIntegralManager alloc] init];
    }
    return _managerIntegral;
}
-(NSArray<SEGBanalcesModel *> *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSArray<SEGBanalcesModel*> array];
    }
    return _dataSource;
}

-(NSArray<SEGBalanceForHbModel *> *)dataSourceCash{
    if (!_dataSourceCash) {
        _dataSourceCash = [NSArray<SEGBalanceForHbModel*> array];
    }
    return _dataSourceCash;
}
@end
